<template>

  <div>
    <h1>图片上传</h1>
    <!-- 控制样式
      list-type 设置照片墙样式
      class 动态类名, 根据当前文件列表长度控制上传按钮显示 -->
    <!-- 钩子
      on-preview 预览
      on-change 修改
      on-remove 删除
      上传功能相关
      http-request 覆盖 action 默认的上传行为 -->
    <el-upload
      action="#"
      :class="{disable:filelist.length>0}"
      list-type="picture-card"
      :file-list="filelist"
      :on-success="onsuccess"
      :on-preview="onPreview"
      :on-remove="onRemove"
      :on-change="onChange"
      :before-upload="beforeupload"
      :http-request="upload"
    >
      <i class="el-icon-plus" />
    </el-upload>
    <!-- 进度条 -->
    <el-progress
      v-if="isShowPercent"
      :text-inside="true"
      :stroke-width="26"
      :percentage="percent"
      style="width: 146px;"
    />
    <el-dialog title="预览" :visible="isShowDialog" @close="isShowDialog = false">
      <el-row type="flex" justify="center">
        <img :src="previewURL" alt="">
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import COS from 'cos-js-sdk-v5'
const cos = new COS({
  SecretId: 'AKIDuaz7yZtwiDRO4TbT4ORDiwtZy6xXv5ya',
  SecretKey: 'NUKtDG2XmkRr0sTosgDSQ8akdFaeQv96'
})
export default {
  data() {
    return {
      filelist: [

      ],
      isShowDialog: false,
      previewURL: '',
      percent: 0,
      //   控制进度条的显示与隐藏
      isShowPercent: false
    }
  },
  methods: {
    onsuccess() {},
    onPreview(file) {
      this.previewURL = file.url
      this.isShowDialog = true
    },
    onRemove(file, newFileList) {
      this.filelist = newFileList
    },
    onChange(file, newFileList) {
      this.filelist = newFileList
    },
    // 文件上传前的钩子
    beforeupload(file) {
      console.log(61, file)
      // 定义一个最大值为2m
      const maxSize = 2 * 1024 * 1024
      if (file.size > maxSize) {
        this.$message.warning('图片大小不能超过 2 M')
        return false
      }
      const whiteList = ['image/jpeg', 'image/png']
      if (!whiteList.includes(file.type)) {
        this.$message.warning('图片暂时只支持  jpeg/ png 格式')
        return false
      }
    },
    upload(params) {
      console.log(80, params)
      this.isShowPercent = true
      cos.putObject({
        Bucket: 'hr-69-1308572360',
        Region: 'ap-guangzhou',
        Key: params.file.name,
        StorageClass: 'STANDARD',
        Body: params.file,
        onProgress: (progressData) => {
          console.log(JSON.stringify(progressData))
          // 显示上传的控制条
          this.percent = progressData.percent * 100
        }
      }, (err, data) => {
        console.log(err || data)
        // 把路径改成网络地址再拼接上http
        this.filelist[0].url = 'http://' + data.Location
        // 上传完毕后修改上传成功的状态
        this.filelist[0].status = 'success'
        setTimeout(() => {
          this.isShowPercent = false
        }, 800)
      })
    }
  }

}
</script>

<style lang="scss" scoped>
.disable {
  // /deep/ .el-upload--picture-card {
  ::v-deep .el-upload--picture-card {
    display: none;
  }
}
</style>
<style lang="scss">
    img{
        object-fit:cover;

    }
</style>
